package com.neusoft.SPNursingHome.controller;

import java.util.concurrent.TimeUnit;

import com.neusoft.SPNursingHome.po.ResponseBean;
import com.neusoft.SPNursingHome.redisDao.RedisDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Redis 操作接口，统一用 ResponseBean 包装返回结果
 */
@RestController
@RequestMapping("/RedisController")
public class RedisController {
	@Autowired
	private RedisDao rd;

	/**
	 * 存值接口，key/value 写入 Redis 并设置过期时间
	 * 示例请求：POST /RedisController/save?key=foo&val=bar
	 */
	@PostMapping("/save")
	public ResponseBean<String> save(@RequestParam String key,
									 @RequestParam String val) {
		rd.set(key, val, 90, TimeUnit.SECONDS);
		// data 字段里放 "ok"
		return new ResponseBean<>( "ok" );
	}

	/**
	 * 读值接口
	 * 示例请求：GET /RedisController/read?key=foo
	 */
	@GetMapping("/read")
	public ResponseBean<String> read(@RequestParam String key) {
		String val = rd.get(key);
		// 如果 key 不存在，val 会是 null，这里也能把 null 放到 data 里
		return new ResponseBean<>( val );
	}
}
